package com.company.cloud.point.modules.mgr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.IdIntParam;
import com.company.cloud.common.bean.IdStrParam;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.point.modules.cache.models.PointRuleConfigModel;
import com.company.cloud.point.modules.cache.service.PointRuleConfigCacheService;
import com.company.cloud.point.modules.mgr.controller.req.PointRuleConfigQuery4PageParam;
import com.company.cloud.point.modules.mgr.controller.req.PointRuleConfigSaveParam;
import com.company.cloud.point.modules.mgr.controller.req.PointRuleConfigUpdateParam;
import com.company.cloud.point.modules.mgr.service.PointRuleConfigMgrService;
import com.company.common.server.config.cloud.ProjectConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Log4j2
@Authc
@RestController
@RequestMapping("/mgr/point/ruleConfig")
@Api(tags="UMP_Mgr_积分规则管理")
public class PointRuleConfigMgrController {

    @Autowired
    private ProjectConfig projectConfig;

    @Autowired
    private PointRuleConfigMgrService pointRuleConfigMgrService;

    @Autowired
    private PointRuleConfigCacheService pointRuleConfigCacheService;

    @PostMapping("/query")
    @ApiOperation(value="积分规则查询", response= PointRuleConfigModel.class)
    public BaseResult query(@RequestBody PointRuleConfigQuery4PageParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        IPage<PointRuleConfigModel> data=pointRuleConfigMgrService.query(params,userInfo);
        return BaseResult.ok(data);
    }


    @PostMapping("/save")
    @ApiOperation(value="积分规则新增", response=BaseResult.class)
    public BaseResult save(@RequestBody PointRuleConfigSaveParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        pointRuleConfigMgrService.save(params,userInfo);
        return BaseResult.ok();
    }


    @PostMapping("/update")
    @ApiOperation(value="积分规则修改", response=BaseResult.class)
    public BaseResult update(@RequestBody PointRuleConfigUpdateParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        pointRuleConfigMgrService.update(params,userInfo);
        pointRuleConfigCacheService.removePointRuleConfigByCache(params.getRuleNo());
        return BaseResult.ok();
    }


    @PostMapping("/info")
    @ApiOperation(value="积分规则信息", response=PointRuleConfigModel.class)
    public BaseResult info(@RequestBody IdStrParam params) {
        ValidatorUtils.validate(params);
        PointRuleConfigModel data= pointRuleConfigCacheService.getPointRuleConfigByDB(params.getId());
        if(data==null){
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        return BaseResult.ok(data);
    }

    @PostMapping("/del")
    @ApiOperation(value="积分规则删除", response=BaseResult.class)
    public BaseResult del(@RequestBody IdStrParam params) {
        ValidatorUtils.validate(params);
        LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
        pointRuleConfigMgrService.del(params.getId(),userInfo);
        pointRuleConfigCacheService.removePointRuleConfigByCache(params.getId());
        return BaseResult.ok();
    }










}
